package com.ruoyi.sbxm.xmjh.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sbxm.xmjh.domain.Xmjh;
import com.ruoyi.sbxm.xmjh.domain.XmjhFj;
import com.ruoyi.sbxm.xmjh.mapper.XmjhMapper;
import com.ruoyi.sbxm.xmjh.service.IXmjhService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 项目计划Service业务层处理
 *
 * @author huacai
 * @date 2024-11-09
 */
@Service
public class XmjhServiceImpl implements IXmjhService {
    @Resource
    private XmjhMapper xmjhMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询项目计划
     *
     * @param xmjhId 项目计划主键
     * @return 项目计划
     */
    @Override
    public Xmjh selectXmjhByXmjhId(String xmjhId) {
        return xmjhMapper.selectXmjhByXmjhId(xmjhId);
    }

    /**
     * 查询项目计划列表
     *
     * @param xmjh 项目计划
     * @return 项目计划
     */
    @Override
    public List<Xmjh> selectXmjhList(Xmjh xmjh) {
        return xmjhMapper.selectXmjhList(xmjh);
    }

    /**
     * 新增项目计划
     *
     * @param xmjh 项目计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertXmjh(Xmjh xmjh) {
        xmjh.setCreateTime(DateUtils.getNowDate());
        int rows = xmjhMapper.insertXmjh(xmjh);
        insertXmjhFj(xmjh);
        return rows;
    }

    /**
     * 批量新增项目计划
     *
     * @param xmjhs 项目计划List
     * @return 结果
     */
    @Override
    public int batchInsertXmjh(List<Xmjh> xmjhs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(xmjhs)) {
            try {
                for (int i = 0; i < xmjhs.size(); i++) {
                    int row = xmjhMapper.insertXmjh(xmjhs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == xmjhs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改项目计划
     *
     * @param xmjh 项目计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateXmjh(Xmjh xmjh) {
        xmjh.setUpdateTime(DateUtils.getNowDate());
//        xmjhMapper.deleteXmjhFjByXmjhId(xmjh.getXmjhId());
        insertXmjhFj(xmjh);
        return xmjhMapper.updateXmjh(xmjh);
    }

    /**
     * 批量删除项目计划
     *
     * @param xmjhIds 需要删除的项目计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteXmjhByXmjhIds(String[] xmjhIds) {
        xmjhMapper.deleteXmjhFjByXmjhIds(xmjhIds);
        return xmjhMapper.deleteXmjhByXmjhIds(xmjhIds);
    }

    /**
     * 删除项目计划信息
     *
     * @param xmjhId 项目计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteXmjhByXmjhId(String xmjhId) {
        xmjhMapper.deleteXmjhFjByXmjhId(xmjhId);
        return xmjhMapper.deleteXmjhByXmjhId(xmjhId);
    }

    /**
     * 新增附件
     * @param xmjhFj
     * @return
     */
    @Override
    public int insertXmjhFjByXmjhId(XmjhFj xmjhFj) {
        return xmjhMapper.insertXmjhFjByXmjhId(xmjhFj);
    }

    /**
     * 根据ID查询附件列表
     * @param xmjhFj
     * @return
     */
    @Override
    public List<XmjhFj> selectXmjhFjList(XmjhFj xmjhFj) {
        return xmjhMapper.selectXmjhFjList(xmjhFj);
    }

    /**
     * 删除项目计划附件
     * @param fjIds
     * @return
     */
    @Override
    public int deleteXmjhFjByFjIds(Integer[] fjIds) {
        return xmjhMapper.deleteXmjhFjByFjIds(fjIds);
    }

    /**
     * 新增项目计划附件信息
     *
     * @param xmjh 项目计划对象
     */
    public void insertXmjhFj(Xmjh xmjh) {
        List<XmjhFj> xmjhFjList = xmjh.getXmjhFjList();
        String xmjhId = xmjh.getXmjhId();
        if (StringUtils.isNotNull(xmjhFjList)) {
            List<XmjhFj> list = new ArrayList<XmjhFj>();
            for (XmjhFj xmjhFj : xmjhFjList) {
                xmjhFj.setXmjhId(xmjhId);
                list.add(xmjhFj);
            }
//            if (list.size() > 0) {
//                xmjhMapper.batchXmjhFj(list);
//            }
        }
    }
}
